#pragma once
#include<iostream>
#include<deque>
#include<vector>
#include<assert.h>

using namespace std;

template <class T, class Container = deque<T>>
class Queue {
public:
	void push(const T& val)
	{
		_con.push_back(val);
	}

	void pop()
	{
		_con.pop_front();
	}

	const T& front()
	{
		return _con.front();
	}

	const T& back()
	{
		return _con.back();
	}

	bool empty()
	{
		return _con.empty();
	}
private:
	Container _con;
};

template <class T>
struct Greater {
	bool operator()(const T& x, const T& y)const
	{
		return x > y;
	}
};

template <class T>
struct Less {
	bool operator()(const T& x, const T& y)const
	{
		return x < y;
	}
};

template <class T, class Container = vector<int>, class Compare = Less<T>>
class Priority_Queue {
public:
	Priority_Queue(const Compare& comfunc=Compare())
		:_comFunc(comfunc)
	{}

	template <class InputIterator>
	Priority_Queue(InputIterator start, InputIterator end, const Compare& comfunc = Compare())
		: _comFunc(comfunc)
	{
		while (start!=end)
		{
			_con.push_back(*start);
			start++;
		}

		for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
		{
			AdjustDown(i);
		}
	}


	void AdjustUp(int child)
	{
		int parent = (child - 1) / 2;
		while (child > 0)
		{
			if (_comFunc(_con[parent],_con[child]))
			{
				swap(_con[parent], _con[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else
			{
				break;
			}
		}
	}

	void AdjustDown(int parent)
	{
		int child = parent * 2 + 1;
		while (child < _con.size())
		{
			if (child + 1 < _con.size() && _comFunc(_con[child], _con[child + 1]))
			{
				child = child + 1;
			}

			if (_comFunc(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				parent = child;
				child = parent * 2 + 1;
			}
			else
			{
				break;
			}
		}
	}

	void push(const T& val)
	{
		_con.push_back(val);
		AdjustUp(_con.size() - 1);
	}

	void pop()
	{
		assert(!_con.empty());
		swap(_con[0], _con[_con.size() - 1]);
		_con.pop_back();

		AdjustDown(0);
	}

	const T& size()
	{
		return _con.size();
	}

	const T& top()
	{
		return _con[0];
	}

	bool empty()
	{
		return _con.empty();
	}

private:
	Container _con;
	Compare _comFunc;
};